package com.stlm2.dems.service;

import com.stlm2.util.codecontent.CodeContent;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;

import com.stlm2.dems.entity.Banner;
import com.stlm2.dems.dao.BannerDao;
import com.stlm2.core.base.BaseDao;
import com.stlm2.core.base.impl.BaseServiceImpl;
import org.springframework.ui.ModelMap;

import java.util.ArrayList;
import java.util.List;

@Service("bannerService")
@Transactional(rollbackForClassName={"java.lang.Exception"}, propagation = Propagation.REQUIRED)
public class BannerService extends BaseServiceImpl<Banner> {

	@Resource
	private BannerDao bannerDao;

	@Override
	protected BaseDao <Banner> getDao() {
		return bannerDao;
	}


	public List<ModelMap> convert(List<Banner> banners){
		List<ModelMap> list = new ArrayList<ModelMap>();
		if(banners!= null &&banners.size() > 0){
			for(Banner banner : banners){
				ModelMap map = buildModel(banner);
				list.add(map);
			}
		}

		return list;
	}

	public ModelMap buildModel(Banner banner){
		ModelMap model = new ModelMap();
		if(banner != null){
			model.put("id", banner.getId());
			model.put("img", banner.getImg());
			model.put("sort", banner.getSort());
			model.put("type",banner.getType()!=null?banner.getType().toString():"");
			model.put("typeValue",banner.getType()!=null?banner.getTypeValue():"");
			model.put("url",banner.getUrl()!=null?banner.getUrl():"#");

		}
		return model;
	}

	/**
	 *  获取首页六张轮播图 wap
	 *  ffl
	 *  2017-06-14 10:57:36
	 * @return
	 */
	public String getSixBannerWap(){
	    List<Banner> banners=bannerDao.getSixBySort();
		return CodeContent.getModelByCode(1,"",convert(banners));
	}


	/**
	 * 获取首页六张轮播图 app
	 * ffl
	 * 2017-08-21 16:04:43
	 * @return
	 */
	public String getSixBannerApp(){
		List<Banner> banners=bannerDao.getSix();
		return CodeContent.getModelByCode(1,"",convert(banners));
	}

	/**
	 * 获取三张轮播图  积分夺宝
	 * ffl
	 * 2017-08-17 10:02:48
	 * @return
	 */
	public String getThree(){
		List<Banner> banners= bannerDao.getThree();
		return CodeContent.getModelByCode(1,"",convert(banners));
	}


	/**
	 * 按类型获取轮播图
	 * ffl
	 * 2017-10-12 10:13:57
	 * @param typeValue
	 * @return
	 */
	public String getByType(String typeValue){
		if (!NumberUtils.isDigits(typeValue)){
			return CodeContent.getModelByCode(0,"请指定轮播图类型",null);
		}
		List<Banner> bannerList=bannerDao.getByType(NumberUtils.toInt(typeValue));
		return CodeContent.getModelByCode(1,"",convert(bannerList));
	}
}